package com.reebake.ideal.message.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.holder.UserContextHolder;
import com.reebake.ideal.message.entity.MessageNotification;
import com.reebake.ideal.message.service.MessageNotificationPlusService;
import com.reebake.ideal.message.service.MessageNotificationService;
import com.reebake.ideal.message.vo.MessageNotificationQueryParamVO;
import com.reebake.ideal.message.vo.MessageNotificationVO;
import com.reebake.ideal.mybatis.core.PageResult;
import com.reebake.ideal.message.vo.SimpleMessageNotificationVO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/message/notification/my")
public class MyMessageNotificationController {
    private final MessageNotificationPlusService messageNotificationPlusService;
    private final MessageNotificationService messageNotificationService;

    @GetMapping("latest")
    public List<SimpleMessageNotificationVO> latest(@RequestParam("pageSize") int pageSize) {
        List<MessageNotification> list = messageNotificationPlusService.latest(UserContextHolder.getCurrentUserId(), pageSize);
        return list.stream().map(item -> {
            SimpleMessageNotificationVO svo = new SimpleMessageNotificationVO();
            svo.setMessage(item.getContent());
            svo.setTitle(item.getTitle());
            svo.setMessageNotificationId(item.getMessageNotificationId());
            svo.setDate(item.getSendTime());
            svo.setAvatar(item.getSenderAvatar());
            svo.setIsRead(item.getIsRead());

            return svo;
        }).collect(Collectors.toList());
    }


    /**
     * 分页查询
     *
     * @param queryParam 查询参数
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("page")
    public PageResult<MessageNotificationVO> page(@RequestBody MessageNotificationQueryParamVO queryParam) {
        LambdaQueryWrapper<MessageNotification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(queryParam.getTitle()), MessageNotification::getTitle, queryParam.getTitle());
        queryWrapper.eq(MessageNotification::getReceiverId, UserContextHolder.getCurrentUserId());

        queryParam = messageNotificationService.page(queryParam, queryWrapper);

        return queryParam.to(MessageNotificationVO.class);
    }

    @Operation(summary = "清空")
    @PostMapping("clear")
    public void clear() {
        messageNotificationPlusService.clear(UserContextHolder.getCurrentUserId());
    }

    @Operation(summary = "全部标记为已读")
    @PostMapping("mark-all")
    public void markAll() {
        messageNotificationPlusService.markAll(UserContextHolder.getCurrentUserId());
    }

    @Operation(summary = "根据编号标记为已读")
    @PostMapping("mark-by-ids")
    public void markByIds(@RequestBody List<String> messageNotificationIds) {
        messageNotificationPlusService.markByIds(messageNotificationIds);
    }

    /**
     * 通过主键批量删除
     *
     * @param messageNotificationIds 主键
     * @return
     */
    @Operation(summary = "通过主键删除多条数据")
    @PostMapping("remove-by-ids")
    public void removeByIds(@RequestBody List<String> messageNotificationIds) {
        messageNotificationService.removeByIds(messageNotificationIds);
    }
}
